package com.zhongke.common.base.activity

import android.os.Bundle
import android.view.View
import androidx.annotation.CallSuper
import androidx.databinding.ViewDataBinding
import com.zhongke.common.R
import com.zhongke.common.base.viewmodel.ZKBaseViewModel
import com.zhongke.common.utils.ZKStatusBarUtil
import com.zhongke.common.widget.TitleView


/**
 * 带有标题栏的基类 Activity，这个标题栏[TitleView]能满足大部分的页面标题栏要求，实现[customTitleView]方法来定制
 * 所需的标题样式，如果不想显示标题栏，调用[TitleView.setStyle]传入[TitleView.Style.NONE]即可
 */
abstract class ZKBaseTitleActivity<V : ViewDataBinding, VM : ZKBaseViewModel<*>> :
    ZKBaseActivity<V, VM>(), View.OnClickListener {

    abstract fun customTitleView(titleView: TitleView)
    override fun isIncludeTitleView() = true

    final override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val titleView = mTitleView
        titleView.apply {
            setOnBackClickListener {
                onBackClick()
            }
            setOnRightTextClickListener {
                onRightTextClick()
            }
            setOnMoreClickListener {
                onMoreClick()
            }
        }
        customTitleView(titleView)
    }

    fun setStatusBarColor(color: Int) {
        window.statusBarColor = color
    }

    fun setDarkMode() {
        ZKStatusBarUtil.setDarkMode(this)
    }

    fun setLightMode() {
        ZKStatusBarUtil.setLightMode(this)
    }

    protected open fun onBackClick() {
        finish()
    }

    protected open fun onMoreClick() {
    }

    protected open fun onRightTextClick() {
    }

    protected fun setRightText(text: String?) {
        mTitleView.setRightText(text)
    }

    protected fun setRightTextEnabled(enabled: Boolean) {
        mTitleView.setRightTextEnabled(enabled)
    }

    protected fun hideBackButton() {
        mTitleView.hideBackButton()
    }

    protected fun showMoreButton(show: Boolean) {
        mTitleView.showMoreButton(show)
    }

    protected fun setTitle(text: String?) {
        mTitleView.setTitle(text)
    }

    protected fun getTitleText() = mTitleView.getTitle()

    @CallSuper
    override fun onClick(v: View) {
        when (v.id) {
            R.id.btnBack -> onBackClick()
            R.id.btnMore -> onMoreClick()
            R.id.btnRight -> onRightTextClick()
        }
    }

}